// merchant//merchants/jjkRecords/index.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    CurrentPage: 1,
    loadingMoreHidden: 1,
    loading: true,
    cardItemId: 0,
    cardRecordsList: []
  },

  getRecords: function(cardItemId){
    let that = this;
    wx.request({
      url: app.globalData.host + '/api/coupon/GetCardUserList?cardItemId='+ cardItemId +'&showMoney=0&page=' + that.data.CurrentPage + '&saleUid=' + app.globalData.userInfo.Uid,
      success: function(res){
        that.setData({
          loading: false
        })
        if(res.data.Code == 0){
          let list = res.data.Data.List;
          if(that.data.CurrentPage != 1){
            let preList = that.data.cardRecordsList;
            list = preList.concat(list);
          }
          if(list.length > 0){
            for(let i = 0; i < list.length; i++){
              list[i].SendDate = list[i].AddTime.substr(0,10);
              list[i].SendTime = list[i].AddTime.substr(11,8);
            }
          }
          that.setData({
            cardRecordsList: list,
            loadingMoreHidden: res.data.Data.PageModel.TotalPages
          })
        }
      },
    })
  },

  loadMore: function(){
    var that = this;
    if (that.data.loadingMoreHidden > 1 && that.data.CurrentPage < that.data.loadingMoreHidden) {
      var currPage = that.data.CurrentPage;
      that.setData({
        CurrentPage: currPage + 1
      })
      that.getRecords(that.data.cardItemId);
    } else {
      return
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.data.cardItemId = options.cardItemId;
    this.getRecords(options.cardItemId);
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.loadMore();
  }
})